package com.jeesite.modules.pt.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.pt.entity.PtIcon;
import com.jeesite.modules.pt.service.PtIconService;

/**
 * 首页ICONController
 * @author 李寒冰
 * @version 2025-04-01
 */
@Controller
@RequestMapping(value = "${adminPath}/pt/ptIcon")
public class PtIconController extends BaseController {

	@Autowired
	private PtIconService ptIconService;

	@Value("${pic.baseUrl}")
	private String baseUrl;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public PtIcon get(String id, boolean isNewRecord) {
		return ptIconService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("pt:ptIcon:view")
	@RequestMapping(value = {"list", ""})
	public String list(PtIcon ptIcon, Model model) {
		model.addAttribute("ptIcon", ptIcon);
		return "modules/pt/ptIconList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("pt:ptIcon:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<PtIcon> listData(PtIcon ptIcon, HttpServletRequest request, HttpServletResponse response) {
		ptIcon.setPage(new Page<>(request, response));
		Page<PtIcon> page = ptIconService.findPage(ptIcon);
		for (int i = 0; i < page.getList().size(); i++) {
			PtIcon ptIcon1 = page.getList().get(i);
			String icon = ptIcon1.getIcon();
			ptIcon1.setIcon(baseUrl+icon);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("pt:ptIcon:view")
	@RequestMapping(value = "form")
	public String form(PtIcon ptIcon, Model model) {
		model.addAttribute("ptIcon", ptIcon);
		return "modules/pt/ptIconForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("pt:ptIcon:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated PtIcon ptIcon) {
		ptIconService.save(ptIcon);
		return renderResult(Global.TRUE, text("保存首页ICON成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("pt:ptIcon:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(PtIcon ptIcon) {
		ptIconService.delete(ptIcon);
		return renderResult(Global.TRUE, text("删除首页ICON成功！"));
	}
	
}